﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRepository;
using Models;
using Infrastructure;
using Spring.Data.Common;
using System.Data;

namespace RepositoryADO
{
    public class TaskRepository : ITaskRepository
    {
        private static string strBase = "select Code, Name, Start_Date, End_Date, Creator, Remark "
            + " from Task_Info ";

        public void CreateTask(TaskInfo info)
        {
            string strSql = "insert into Task_Info(Code, Name, Start_Date, End_Date, Creator, Remark) "
                + " values(@Code, @Name, @Start_Date, @End_Date, @Creator, @Remark) ";

            string strSearch = "SELECT isnull(max(Code),'T000000000') FROM Task_Info ";
            string strMaxNo = DBHelper.SpringDBTemplate.ExecuteScalar(CommandType.Text, strSearch).ToString();
            int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
            info.Code = "T" + nNum.ToString("000000000");

            IDbParameters para = DBHelper.SpringDBTemplate.CreateDbParameters();
            para.Add("Code", DbType.String).Value = info.Code;
            para.Add("Name", DbType.String).Value = info.Name;
            para.Add("Start_Date", DbType.DateTime).Value = info.StartDate;
            para.Add("End_Date", DbType.DateTime).Value = info.EndDate;
            para.Add("Creator", DbType.String).Value = info.Creator;
            para.Add("Remark", DbType.String).Value = info.Remark;

            DBHelper.SpringDBTemplate.ExecuteScalar(CommandType.Text, strSql, para);
        }


        public TaskInfo GetTaskInfo(string code)
        {
            throw new NotImplementedException();
        }


        public void Update(TaskInfo info)
        {
            throw new NotImplementedException();
        }
    }
}
